package com.example.e;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    CheckBox checkBox;
    
    Switch swch1;

    EditText edt1;

    RadioGroup rg1;
    RadioButton rb1;
    RadioButton rb2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        checkBox = findViewById(R.id.ckb1);
        swch1 = findViewById(R.id.sw1);
        rg1 = findViewById(R.id.rg1);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        edt1 = findViewById(R.id.edt1);

        if(checkBox.isChecked()){
            Toast.makeText(this,"选中了",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"没选中",Toast.LENGTH_SHORT).show();
        }

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(checkBox.isChecked()){
                    Toast.makeText(MainActivity.this,"选中了",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"没选中",Toast.LENGTH_SHORT).show();
                }
            }
        });

        swch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    buttonView.setText("开启");
                }else{
                    buttonView.setText("关闭");
                }
            }
        });

        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                Toast.makeText(MainActivity.this,checkedRadioButtonId+"选中",Toast.LENGTH_SHORT).show();

                RadioButton rb = (RadioButton) group.getChildAt(0);
                int id = rb.getId();
                if(id == checkedId){
                    Toast.makeText(MainActivity.this,"男",Toast.LENGTH_SHORT).show();
                }

                rb = (RadioButton) group.getChildAt(1);
                id = rb.getId();
                if(id == checkedId){
                    Toast.makeText(MainActivity.this,"女",Toast.LENGTH_SHORT).show();
                }
            }
        });


        edt1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {



                return false;
            }
        });
    }
}